package com.atguigu.filter.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class Filter01HelloWorld extends HttpFilter {

    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("-----------------------Filter01HelloWorld 开始执行-----------------------");
        // 目标：访问私密资源的请求，必须满足一定的条件
        // 条件：请求携带请求参数，monster=peter
        // 1、获取请求参数
        String monster = request.getParameter("monster");

        // 2、检查 monster 的值是否等于 peter
        if ("peter".equals(monster)) {
            // 3、满足条件：放行
            // 从技术角度解释放行：让当前请求继续执行，继续正常访问它原本要访问那个资源
            chain.doFilter(request, response);
        } else {
            // 4、不满足条件：由当前 Filter 给出一个响应
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write("过滤条件不满足，请重新访问！");
        }
        System.out.println("-----------------------Filter01HelloWorld 结束执行-----------------------");
    }
}
